/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;
import DTO.CFields;
import DTO.CNhanVien;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CNhanVienDAO extends IData<CNhanVien>{
    public CNhanVienDAO(){}

    @Override
    protected Object OnSubmit(CNhanVien item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CNhanVien getItem(ResultSet rs) {
        CNhanVien item = new CNhanVien();
        try {
            item.setChuc_VuId(rs.getInt(CFields.chuc_vu_id));
            item.setGioiTinh(rs.getBoolean(CFields.gioi_tinh));
            item.setHoTen(rs.getString(CFields.hoten));
            item.setId(rs.getLong(CFields.id));
            item.setMaNhanVien(rs.getString(CFields.ma_nhan_vien));
            item.setDiaChi(rs.getString(CFields.dia_chi));
            item.setDienThoai(rs.getString(CFields.dien_thoai));
            item.setNgaySinh(rs.getDate(CFields.ngay_sinh));           
        } catch (SQLException ex) {
            Logger.getLogger(CNhanVienDAO.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return item;
    }

    @Override
    public List<CNhanVien> getAll(SubConfig config) {
        //String mysql = "select * from " + CFields.tb_nhan_vien ;
        String mysql = "select * from " + CFields.tb_nhan_vien + sWhere + " order by id desc ";
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CNhanVien item, SubConfig config) {
        Object result =0 ;
        String mysql = "insert into "+ CFields.tb_nhan_vien + "("
        + CFields.user_id + "," + CFields.hoten + "," + CFields.dia_chi + ","
        + CFields.dien_thoai + "," + CFields.chuc_vu_id + ","
        + CFields.ma_nhan_vien +  "," + CFields.gioi_tinh + "," + CFields.ngay_sinh                
        +") values(?,?,?,?,?,?,?,?)" ;
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setLong(1, item.getUserId());
            cs.setString(2, item.getHoTen());
            cs.setString(3, item.getDiaChi());            
            cs.setString(4, item.getDienThoai());
            cs.setLong(5, item.getChucVuId());
            cs.setString(6, item.getMaNhanVien());            
            cs.setBoolean(7, item.getGioiTinh());
            java.util.Date birth = item.getNgaySinh();
            java.sql.Date temp = new java.sql.Date(birth.getYear(),birth.getMonth(),birth.getDate());
            cs.setDate(8, temp);
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(CFields.id);
            }
        } catch (SQLException ex) {
            
        }
        
        return result;
    }

    @Override
    public Object updateOnSubmint(CNhanVien item, SubConfig config) {
        Object result = 0;
        String mysql ="update " + CFields.tb_nhan_vien + " set "                                
                + CFields.hoten + "=?," + CFields.dia_chi + "=?,"
                + CFields.dien_thoai + "=?," + CFields.chuc_vu_id + "=?,"
                + CFields.gioi_tinh + "=?," + CFields.ngay_sinh +"=? where " + CFields.id + "=?"
                ;
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);            
            cs.setString(1, item.getHoTen());
            cs.setString(2, item.getDiaChi());            
            cs.setString(3, item.getDienThoai());
            cs.setLong(4, item.getChucVuId());            
            cs.setBoolean(5, item.getGioiTinh());
            java.util.Date birth = item.getNgaySinh();
            java.sql.Date temp = new java.sql.Date(birth.getYear(),birth.getMonth(),birth.getDate());
            cs.setDate(6, temp);
            cs.setLong(7, item.getId());
            result = cs.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CNhanVienDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public Object deleteOnSubmint(CNhanVien item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   

    @Override
    public CNhanVien Find(CNhanVien item, List<CNhanVien> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CNhanVien add(CNhanVien item) {
        try {
            ps.setLong(1, item.getUserId());
            ps.setString(2, item.getHoTen());
            ps.setString(3, item.getDiaChi());            
            ps.setString(4, item.getDienThoai());
            ps.setLong(5, item.getChucVuId());
            ps.setString(6, item.getMaNhanVien());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CNhanVienDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CNhanVien> list, SubConfig config) {
        String mysql = "insert into "+ CFields.tb_nhan_vien + "("
        + CFields.user_id + "," + CFields.hoten + "," + CFields.dia_chi + ","
        + CFields.dien_thoai + "," + CFields.chuc_vu_id
        +") values(?,?,?,?,?)" ;
        int rs = 0;
        try {
            ps = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            for(CNhanVien item : list){
                this.add(item);
            }
           int []n = ps.executeBatch();   
           rs = n.length;
        } catch (SQLException ex) {
            
        }
        return rs;
    }

    @Override
    protected void add(CNhanVien item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}
